<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>收藏夹新增</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/html/layui/css/layui.css" media="all">
  <style type="text/css">
  .layui-form-item .layui-input-inline {
    width:400px;
    }
    .layui-form-item{
        max-width:600px;
    }
    </style>
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
  <div class="layui-form-item">
    <label class="layui-form-label">标签</label>
    <div class="layui-input-inline">
      <div name="tagIds" id="tagIds"></div>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">收藏名称</label>
    <div class="layui-input-inline">
      <input type="text" name="name" id="name" lay-verify="required" placeholder="请输入收藏名称" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">是否公开</label>
    <div class="layui-input-inline">
      <input type="checkbox" name="private" id="private" lay-skin="switch" lay-text="公开|私有"   >
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">描述</label>
    <div class="layui-input-inline">
      <textarea name="remark" placeholder="请输入描述" class="layui-textarea"></textarea>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">链接</label>
    <div class="layui-input-inline">
      <input type="text" name="link" id="link"  placeholder="请输入链接" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item "  style="text-align:right;">
    <input type="hidden" name="id" id="id" />
    <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" class="layui-btn layui-btn-normal" value="确认添加">
  </div>
</div>

<script src="/html/layui/layui.js"></script>
<script src="/html/lib/xmselect/xm-select.js"></script>
<script>
  var selAppId =null;
  var selectList=null;
  layui.config({
    base: '/html/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$
    ,form = layui.form;
    selAppId = xmSelect.render({
    	el: '#tagIds',
    	name:'tagIds',
    	//配置搜索
    	filterable: true,
    	radio: false,
    	//clickClose: true,
    	//配置远程分页
    	paging: true,
    	pageRemote: true,
    	pageEmptyShow: false,
        on:function(a,b,c){
            if(a.isAdd){
                if(a.change){
                    $.each(a.change,function(index,a){
                        if(a.value && a.value!=-1){
                            return;
                        }
                        if(!a.name1){
                            return false;
                        }
                        var field={
                            "name":a.name1
                        }
                        $.ajax({
                            async : true,
                            "url" : "/api/scj/tag/add",
                            "global" : false,
                            "type" : "POST",
                            "contentType": "application/json; charset=utf-8",
                            "dataType" : "json",
                            "timeout" : "60000",
                            "data" : JSON.stringify(field),
                            "traditional":true,
                            "success" : function(a1,b,c){
                                if(a1.code==200){
//                                     a.name=a.name1;
//                                     a.value=a1.v;
//                                     selAppId.setValue([
//                                             {name: a.name1, value: a.v},
//                                         ])
                                    $.each(selectList,function(i,data){
                                        if(data.value==-1){
                                            data.name=data.name1;
                                            data.value=a1.v;
                                            selAppId.update(selectList);
                                        }
                                    });

                                    layer.msg('新增成功!'+a1.v);
                                }else{
                                    layer.msg('新增失败');
                                    return false;
                                }
                            },
                            "error" : function(err, msg, code) {
                                return false;
                            }
                        });
                    });
                }
            }
        },
        create: function(val, arr){
//             if(arr.length === 0){
//                 return {
//                     name: '' + val,
//                     value: null
//                     }
//             }

        },
    	model: {
    		icon: 'hidden',
    		label: {
    			type: 'block'
    		}
    	},
    	toolbar:{
    		show: false,
    	},
    	//数据处理
    	remoteMethod: function(val, cb, show, pageIndex){
    		//val: 搜索框的内容, 不开启搜索默认为空, cb: 回调函数, show: 当前下拉框是否展开, pageIndex: 当前第几页
    		var field={"appName":val};
    		var pageSize=10;
        	layui.$.ajax({
    			async : true,
    			"url" : "/api/scj/tag/page?page="+pageIndex+"&limit="+pageSize+"&name="+val,
    			"global" : false,
    			"type" : "POST",
    			"contentType": "application/json; charset=utf-8",
    			"dataType" : "json",
    			"timeout" : "60000",
    			"traditional":true,
    			"success" : function(a,b,c){
    				if(a.v&&a.v.collection){
    					var data=[]
                        if(val){
                            data.push({"name":val+"（点击新增）","value":-1,"name1":val});
                        }
    					$.each(a.v.collection,function(index,item){
    						var item={"name":item.name,"value":item.id};
    						data.push(item);
//     						$("#appId").append('<option value="'+item.id+'">'+item.name+'</option>');
    					});
    					cb(data, a.v.totalPages)
                        selectList=data;
    				}
    				
    			},
    			"error" : function(err, msg, code) {
    				return false;
    			}
    		});
    	}
    })
    
    function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
		return (false);
	}
//     selAppId.
	var id = getQueryVariable("id");
    var url = getQueryVariable("url");
    var title=getQueryVariable("title");
    if(url){
        $("#link").val(decodeURIComponent(url));
        $("#name").val(decodeURIComponent(title));
    }
	if(id){
		$.ajax({
			async : true,
			"url" : "/api/scj/sc/get?id="+id,
			"global" : false,
			"type" : "GET",
			"contentType": "application/json; charset=utf-8",
			"dataType" : "json",
			"timeout" : "60000",
			"traditional":true,
			"success" : function(a,b,c){
				if(a.v){
					var data=a.v[0]
					//var tagIds=data.tagIds;
					//selType.setValue([data.type])
					$("#id").val(data.id)
					$("#name").val(data.name)
					$("#remark").val(data.remark)
              $("#link").val(data.link)
					//getAppById(appId)
				}
			},
			"error" : function(err, msg, code) {
				return false;
			}
		});
      getAppById(id)
	}
	
	
	function getAppById(id){
		$.ajax({
			async : true,
			"url" : "/api/scj/sc/tag?scId="+id,
			"global" : false,
			"type" : "GET",
			"contentType": "application/json; charset=utf-8",
			"dataType" : "json",
			"timeout" : "60000",
			"traditional":true,
			"success" : function(a,b,c){
				if(a.v){
              var rs=[]
              layui.$.each(a.v,function(a,b){
                rs.push({name: b.name, value: b.tagId, selected: true})
              });
					selAppId.setValue(rs)
				}
			},
			"error" : function(err, msg, code) {
				return false;
			}
		});
	}
    
	
	
    
//     loadAppList();
    
    function loadAppList(){
    	var field={};
    	$.ajax({
			async : true,
			"url" : "/api/app/page?page=1&limit=999",
			"global" : false,
			"type" : "POST",
			"contentType": "application/json; charset=utf-8",
			"dataType" : "json",
			"timeout" : "60000",
			"data" : JSON.stringify(field),
			"traditional":true,
			"success" : function(a,b,c){
				if(a.v&&a.v.collection){
					$.each(a.v.collection,function(index,item){
// 						$("#appId").append('<option value="'+item.id+'">'+item.appName+'</option>');
					});
				}
				
			},
			"error" : function(err, msg, code) {
				return false;
			}
		});
    }
    //监听提交
    form.on('submit(layuiadmin-app-form-submit)', function(data){
    	debugger
      var field = data.field; //获取提交的字段
      //if(selAppId.getValue()){
	  	//  field.tagIds=selAppId.getValue();
      //}
      var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  
			$.ajax({
		async : true,
		"url" : "/api/scj/sc/add",
		"global" : false,
		"type" : "POST",
		"contentType": "application/json; charset=utf-8",
		"dataType" : "json",
		"timeout" : "60000",
		"data" : JSON.stringify(field),
		"traditional":true,
		"success" : function(a,b,c){
          layer.msg('新增成功');
			//parent.layui.table.reload('LAY-app-content-list'); //重载表格
      		//parent.layer.close(index); //再执行关闭 
		},
		"error" : function(err, msg, code) {
			return false;
		}
	});
      //提交 Ajax 成功后，关闭当前弹层并重载表格
      //$.ajax({});
      
    });
  })
  </script>
</body>
</html>
